{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "import matplotlib.pyplot as plt\n",
    " \n",
    "# prepare dataset\n",
    "xy = np.loadtxt('dataset/diabetes.csv', delimiter=',', dtype=np.float32)\n",
    "x_data = torch.from_numpy(xy[:, :-1]) # 第一个‘：’是指读取所有行，第二个‘：’是指从第一列开始，最后一列不要\n",
    "y_data = torch.from_numpy(xy[:, [-1]]) # [-1] 最后得到的是个矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# design model using class\n",
    "class Model(torch.nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Model, self).__init__()\n",
    "        self.linear1 = torch.nn.Linear(8, 6) # 输入数据x的特征是8维，x有8个特征\n",
    "        self.linear2 = torch.nn.Linear(6, 4)\n",
    "        self.linear3 = torch.nn.Linear(4, 1)\n",
    "        self.sigmoid = torch.nn.Sigmoid() # 激活函数\n",
    " \n",
    "    def forward(self, x):\n",
    "        x = self.sigmoid(self.linear1(x))\n",
    "        x = self.sigmoid(self.linear2(x))\n",
    "        x = self.sigmoid(self.linear3(x)) # y hat\n",
    "        return x\n",
    "model = Model()         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# construct loss and optimizer\n",
    "# criterion = torch.nn.BCELoss(size_average = True)\n",
    "criterion = torch.nn.BCELoss(reduction='mean')  \n",
    "optimizer = torch.optim.SGD(model.parameters(), lr=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0.7446860671043396\n",
      "1 0.7349317073822021\n",
      "2 0.726140022277832\n",
      "3 0.7182179689407349\n",
      "4 0.7110802531242371\n",
      "5 0.7046497464179993\n",
      "6 0.6988564729690552\n",
      "7 0.693636953830719\n",
      "8 0.6889341473579407\n",
      "9 0.6846963167190552\n",
      "10 0.6808767318725586\n",
      "11 0.6774337291717529\n",
      "12 0.6743295192718506\n",
      "13 0.67153000831604\n",
      "14 0.6690048575401306\n",
      "15 0.6667264699935913\n",
      "16 0.6646705865859985\n",
      "17 0.6628146767616272\n",
      "18 0.6611390113830566\n",
      "19 0.6596256494522095\n",
      "20 0.6582584977149963\n",
      "21 0.6570231914520264\n",
      "22 0.6559066772460938\n",
      "23 0.6548972725868225\n",
      "24 0.6539844870567322\n",
      "25 0.653158962726593\n",
      "26 0.6524121165275574\n",
      "27 0.6517362594604492\n",
      "28 0.6511245369911194\n",
      "29 0.6505707502365112\n",
      "30 0.6500693559646606\n",
      "31 0.6496151685714722\n",
      "32 0.6492037773132324\n",
      "33 0.6488311290740967\n",
      "34 0.6484932899475098\n",
      "35 0.6481871008872986\n",
      "36 0.6479096412658691\n",
      "37 0.6476579308509827\n",
      "38 0.6474297642707825\n",
      "39 0.6472227573394775\n",
      "40 0.6470350027084351\n",
      "41 0.6468645930290222\n",
      "42 0.6467100381851196\n",
      "43 0.6465697288513184\n",
      "44 0.6464422941207886\n",
      "45 0.6463266611099243\n",
      "46 0.6462216377258301\n",
      "47 0.646126389503479\n",
      "48 0.6460397839546204\n",
      "49 0.6459611058235168\n",
      "50 0.6458896994590759\n",
      "51 0.645824670791626\n",
      "52 0.6457656621932983\n",
      "53 0.6457119584083557\n",
      "54 0.6456632018089294\n",
      "55 0.6456187963485718\n",
      "56 0.6455784440040588\n",
      "57 0.6455417275428772\n",
      "58 0.6455084681510925\n",
      "59 0.6454780697822571\n",
      "60 0.6454504132270813\n",
      "61 0.6454252004623413\n",
      "62 0.6454023718833923\n",
      "63 0.6453815698623657\n",
      "64 0.6453624963760376\n",
      "65 0.6453450918197632\n",
      "66 0.6453294157981873\n",
      "67 0.6453150510787964\n",
      "68 0.645301878452301\n",
      "69 0.6452898979187012\n",
      "70 0.6452789306640625\n",
      "71 0.645268976688385\n",
      "72 0.6452599167823792\n",
      "73 0.6452515721321106\n",
      "74 0.6452438831329346\n",
      "75 0.6452368497848511\n",
      "76 0.6452305912971497\n",
      "77 0.6452247500419617\n",
      "78 0.6452193260192871\n",
      "79 0.6452144384384155\n",
      "80 0.6452099084854126\n",
      "81 0.6452056765556335\n",
      "82 0.6452019810676575\n",
      "83 0.6451984643936157\n",
      "84 0.6451951265335083\n",
      "85 0.6451921463012695\n",
      "86 0.6451894640922546\n",
      "87 0.6451867818832397\n",
      "88 0.6451845169067383\n",
      "89 0.6451822519302368\n",
      "90 0.6451802253723145\n",
      "91 0.6451783180236816\n",
      "92 0.6451764106750488\n",
      "93 0.6451749205589294\n",
      "94 0.6451733112335205\n",
      "95 0.6451718807220459\n",
      "96 0.6451704502105713\n",
      "97 0.645169198513031\n",
      "98 0.6451680064201355\n",
      "99 0.6451668739318848\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYgAAAEGCAYAAAB/+QKOAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAhaUlEQVR4nO3de5xcdX3/8ddnZvaWvWR3s7u5bK7kHiIkJETkoggFARVsf0JDUcBq6a8ttvArtqi1Wmr7qy39WdE8VLxUrGiKKBoKGCxQQIGQDUnIndxIsrlurpvNZq/z+f0xZ8NkM5tssjt7Zmfez4f7yJzv+c7M53jIvnO+55zvMXdHRESku0jYBYiISGZSQIiISEoKCBERSUkBISIiKSkgREQkpVjYBfSXqqoqHz9+fNhliIgMKsuWLdvv7tWp1mVNQIwfP566urqwyxARGVTMbFtP6zTEJCIiKSkgREQkJQWEiIikpIAQEZGUFBAiIpKSAkJERFJSQIiISEo5HxBHmtv52n9v5M36w2GXIiKSUbLmRrlzZRH46n+/RV7MuGB0edjliIhkjJw/gigrzGN4WQGb9jWFXYqISEbJ+YAAmF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WEABdM6pCIiy+bWYPBJ9x8wBuhkivaTZXkT4ysyZ3Lwm7DpH+piEmERFJSUcQIiKSko4gREQkJQWEiIikpIAQEZGUFBAiIpKSAkJERFL6/zY7S9bkihFgAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "epoch_list = []\n",
    "loss_list = []\n",
    "# training cycle forward, backward, update\n",
    "for epoch in range(100):\n",
    "    y_pred = model(x_data)\n",
    "    loss = criterion(y_pred, y_data)\n",
    "    print(epoch, loss.item())\n",
    "    epoch_list.append(epoch)\n",
    "    loss_list.append(loss.item())\n",
    " \n",
    "    optimizer.zero_grad()\n",
    "    loss.backward()\n",
    " \n",
    "    optimizer.step()\n",
    " \n",
    " \n",
    "plt.plot(epoch_list, loss_list)\n",
    "plt.ylabel('loss')\n",
    "plt.xlabel('epoch')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "layer1_weight tensor([[ 1.7651e-01,  3.5330e-01,  1.4792e-01,  2.9348e-01, -2.8833e-01,\n",
      "         -3.0050e-01, -1.5043e-01,  1.5000e-01],\n",
      "        [-2.1840e-01, -2.2123e-01, -7.0649e-02, -2.5516e-01,  1.4408e-01,\n",
      "          8.3131e-02,  2.5558e-01,  5.4092e-02],\n",
      "        [-4.0210e-02,  2.5197e-01, -6.7947e-03, -2.8301e-02, -1.8195e-01,\n",
      "         -2.7022e-01,  1.9882e-01, -1.1683e-01],\n",
      "        [-2.0424e-01, -1.6467e-01,  1.3063e-01, -2.6429e-01,  1.7695e-01,\n",
      "          2.0174e-01,  2.3443e-01, -1.2259e-01],\n",
      "        [-3.2068e-01,  2.9032e-01, -2.4064e-01,  3.4164e-04,  2.6900e-01,\n",
      "         -2.5522e-01,  2.8903e-01,  5.7032e-02],\n",
      "        [ 1.2439e-01, -3.2358e-01,  3.4076e-01,  2.8417e-01,  2.4986e-01,\n",
      "          1.4035e-01,  8.7981e-02, -1.8047e-01]])\n",
      "layer1_weight.shape torch.Size([6, 8])\n",
      "layer1_bias tensor([-0.0231, -0.1898, -0.3338, -0.0917, -0.2560, -0.2929])\n",
      "layer1_bias.shape torch.Size([6])\n"
     ]
    }
   ],
   "source": [
    "# 打印参数\n",
    "layer1_weight = model.linear1.weight.data\n",
    "layer1_bias = model.linear1.bias.data\n",
    "print(\"layer1_weight\", layer1_weight)\n",
    "print(\"layer1_weight.shape\", layer1_weight.shape)\n",
    "print(\"layer1_bias\", layer1_bias)\n",
    "print(\"layer1_bias.shape\", layer1_bias.shape)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
